//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.elitecrm.webchat.robot;

import com.elitecrm.webchat.model.Message;
import com.elitecrm.webchat.model.Session;
import com.elitecrm.webchat.model.User;
import com.elitecrm.webchat.service.http.PoolingHttpService;
import com.elitecrm.webchat.util.StringUtil;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;

public class SobotRobotEngine extends RobotEngine {
    private static final String URL_TIKEN_GET = "?appId={0}&createTime={1}&sign={2}&expire={3}";
    private String tokenUrl = "https://open.sobot.com/open/platform/getAccessToken.json";
    private String appid = "fc138964978c488596d2899a1ba3b081";
    private String appKey = "CS4ay17FJ1IR";
    private static final int EXPRIRE = 24;
    private String serverUrl;
    private String sysNum;
    private String partnerId;
    private String question;
    private String uname;
    private String requestText;
    private String s_source;
    private String s_channel_flag;
    private int source;
    private int robotFlag;
    private int questionFlag;
    private String userId;
    private String sessionId;
    private String from;

    public String getTokenUrl() {
        return this.tokenUrl;
    }

    public void setTokenUrl(String tokenUrl) {
        this.tokenUrl = tokenUrl;
    }

    public String getAppid() {
        return this.appid;
    }

    public void setAppid(String appid) {
        this.appid = appid;
    }

    public String getAppKey() {
        return this.appKey;
    }

    public void setAppKey(String appKey) {
        this.appKey = appKey;
    }

    public String getServerUrl() {
        return this.serverUrl;
    }

    public void setServerUrl(String serverUrl) {
        this.serverUrl = serverUrl;
    }

    public String getSysNum() {
        return this.sysNum;
    }

    public void setSysNum(String sysNum) {
        this.sysNum = sysNum;
    }

    public String getPartnerId() {
        return this.partnerId;
    }

    public void setPartnerId(String partnerId) {
        this.partnerId = partnerId;
    }

    public String getQuestion() {
        return this.question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public String getUname() {
        return this.uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getRequestText() {
        return this.requestText;
    }

    public void setRequestText(String requestText) {
        this.requestText = requestText;
    }

    public String gets_source() {
        return this.s_source;
    }

    public void sets_source(String s_source) {
        this.s_source = s_source;
    }

    public String gets_channel_flag() {
        return this.s_channel_flag;
    }

    public void sets_channel_flag(String requestText) {
        this.s_channel_flag = s_channel_flag;
    }

    public int getSource() {
        return this.source;
    }

    public void setSource(int source) {
        this.source = source;
    }

    public int getRobotFlag() {
        return this.robotFlag;
    }

    public void setRobotFlag(int robotFlag) {
        this.robotFlag = robotFlag;
    }

    public int getQuestionFlag() {
        return this.questionFlag;
    }

    public void setQuestionFlag(int questionFlag) {
        this.questionFlag = questionFlag;
    }

    public SobotRobotEngine() {
    }

    public SobotRobotEngine(JSONObject recJSON, Session session) {
        if (recJSON.has("name")) {
            this.robotEnginName = recJSON.getString("name");
        } else {
            this.robotEnginName = "智齿";
        }

        this.id = "BOT004";
        this.type = 4;
        JSONObject propertyJSON = recJSON.getJSONObject("property");
        String appId = propertyJSON.optString("appId");
        String appkey = propertyJSON.optString("appkey");
        int robotFlag = propertyJSON.optInt("robotFlag");
        String sysNum = propertyJSON.optString("sysNum");
        String partnerId = propertyJSON.optString("partnerId");
        this.serverUrl = recJSON.getString("serverUrl");
        this.s_source = propertyJSON.getString("source");
        this.s_channel_flag = propertyJSON.getString("channel_flag");
        this.tokenUrl = recJSON.getString("tokenUrl");
        this.tokenUrl = this.tokenUrl + "?appId={0}&createTime={1}&sign={2}&expire={3}";
        this.appid = appId;
        this.appKey = appkey;
        this.sysNum = sysNum;
        this.robotFlag = robotFlag;
        this.partnerId = partnerId;
        this.accessToken = this.getAccessToken();
        User client = session.getClient();
        this.userId = client.getId();
        this.sessionId = session.getId() + "";
        this.from = client.getFrom();
    }

    public String search(Message msg, long sessionId) {
        try {
            String question = msg.getMessage();
            new HashMap();
            String reqParam = this.getJSONBody(question);
            RobotUtils.printLog("Call sorobot request: " + reqParam);
            Map<String, String> headers = new HashMap();
            String responseStr = PoolingHttpService.getInstance().postStr(this.serverUrl, headers, reqParam);
            RobotUtils.printLog("search result " + responseStr);
            return responseStr;
        } catch (Exception var9) {
            RobotUtils.printErrorLog(var9);
            return "";
        }
    }

    public String getFeedBackJSONBody(String status, String docid, String docName, String msgid, String uid, String cid) {
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("action", "robot_feedback");
        jsonBody.put("access_token", this.accessToken);
        JSONObject dataJSON = new JSONObject();
        if (StringUtil.isNotNull(uid)) {
            dataJSON.put("uid", uid);
        } else {
            dataJSON.put("sysNum", this.sysNum);
            dataJSON.put("partnerid", this.partnerId);
            dataJSON.put("from", this.from);
        }

        dataJSON.put("cid", cid);
        dataJSON.put("docId", docid);
        dataJSON.put("docName", docName);
        dataJSON.put("robotFlag", this.robotFlag);
        dataJSON.put("status", status);
        dataJSON.put("msgId", msgid);
        jsonBody.put("data", dataJSON);
        return jsonBody.toString();
    }

    public String feedback(String feedback) {
        if (StringUtil.isNotNull(this.feedBackUrl)) {
            JSONObject jsonFeedback = JSONObject.fromObject(feedback);
            String status = jsonFeedback.getString("status");
            String docid = jsonFeedback.getString("docid");
            String docName = jsonFeedback.getString("docname");
            String uid = jsonFeedback.getString("uid");
            String cid = jsonFeedback.getString("cid");
            String msgid = jsonFeedback.getString("msgid");
            Map<String, String> headers = new HashMap();
            String params = this.getFeedBackJSONBody(status, docid, docName, msgid, uid, cid);
            String responseStr = PoolingHttpService.getInstance().post(this.feedBackUrl, headers, params);
            RobotUtils.printLog("params: " + params + ", feedback result: " + responseStr);
        }

        return null;
    }

    public String getJSONBody(String question) {
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("action", "chat_user_robot_ask");
        jsonBody.put("access_token", this.accessToken);
        JSONObject dataJSON = new JSONObject();
        dataJSON.put("sysNum", this.sysNum);
        dataJSON.put("partnerId", this.userId);
        dataJSON.put("question", question);
        dataJSON.put("source", Integer.toString(this.source));
        dataJSON.put("robotFlag", Integer.toString(this.robotFlag));
        dataJSON.put("questionFlag", Integer.toString(this.questionFlag));
        dataJSON.put("requestText", this.requestText);

        if (this.s_source != null && this.s_source.length() > 0) {
            dataJSON.put("s_source", this.s_source);
        }
        if (this.s_channel_flag != null && this.s_channel_flag.length() > 0) {
            dataJSON.put("s_channel_flag", this.s_channel_flag);
        }
        jsonBody.put("data", dataJSON);
        return jsonBody.toString();
    }

    public String getAccessToken() {
        String createTime = Long.toString(System.currentTimeMillis());
        this.tokenUrl = MessageFormat.format(this.tokenUrl, this.appid, createTime, new String(DigestUtils.md5Hex(this.appid + this.appKey + createTime)), 24);
        RobotUtils.printLog("tokenUrl " + this.tokenUrl);
        String tokenStr = PoolingHttpService.getInstance().get(this.tokenUrl);
        if (StringUtil.isNotNull(tokenStr)) {
            JSONObject tokenJSON = JSONObject.fromObject(tokenStr);
            if (tokenJSON.has("code") && "1000".equals(tokenJSON.getString("code"))) {
                return tokenJSON.getJSONObject("data").getString("access_token");
            }
        }

        return "";
    }

    public JSONObject translateAnswer(String result) {
        if (StringUtil.isNotNull(result)) {
            JSONObject resultJSON = JSONObject.fromObject(result);
            String code = resultJSON.getString("code");
            if ("1000".equals(code)) {
                JSONObject data = resultJSON.getJSONObject("data");
                String answer = data.getString("answer");
                RobotUtils.printLog("答案是： " + answer);
            } else {
                RobotUtils.printLog("接口异常");
            }
        } else {
            RobotUtils.printLog("结果为空");
        }

        return null;
    }
}
